// pages/Census/data-detail/index.js
const Controllers = require('../../controllers/ControllerCensus.js').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: [{
        laber: '总收益',
        value: '0',
        key: "totalMoney",
        unit: '¥',
        type: '7,8,10,12,13,14,15,16,17,18,20,22,23',
        className: '',
      },
      {
        laber: '租金收益',
        value: '0',
        key: 'rentMoney',
        unit: '¥',
        type: '8,13',
        className: 'fontColor',
        childs: [{
          laber: '全部',
          type: '8,13',
        }, {
          laber: '入驻续费',
          type: '8',
        }, {
          laber: '商圈开通',
          type: '13',
        }]
      },
      {
        laber: '运营收益',
        value: '0',
        key: 'operateMoney',
        unit: '¥',
        type: '14,15,18',
        className: 'fontColor',
        childs: [{
          laber: '全部',
          type: '14,15,18',
        }, {
          laber: '设计海报',
          type: '14',
        }, {
          laber: '定制店铺',
          type: '15',
        }, {
          laber: '店铺托管',
          type: '18',
        }]
      },
      {
        laber: '商圈活动收益',
        value: '0',
        key: 'groupMoney',
        unit: '¥',
        type: '10,12,20,23',
        className: 'fontColor',
        childs: [{
          laber: '全部',
          type: '10,12,20,23',
        }, {
          laber: '一卡通',
          type: '10',
        }, {
          laber: '店铺爆款',
          type: '12',
        }, {
          laber: '超值礼包',
          type: '23',
        }, {
          laber: '欢乐拼',
          type: '20',
        }
        // , {
        //   laber: '代金券收益',
        //   type: '22',
        // }
      ]},
      {
        laber: '云商品收益',
        value: '0',
        key: 'cloudMoney',
        unit: '¥',
        type: '7',
        className: 'fontColor'
      }
    ],
    fundDetail: '',
    active: 0,
    currentIndex: 0,
    childsIndex: 0,
    pageNumber: 1,
    list: [],
    totalRow: 0,
    isLoading: false,
    type: '',
    lastPage: true,
    startTime: null,
    endTime: null,
  },
  onLoad: function (options) {
    this.getMoneyStatistics()
    this.moneyStatisticsPage();
  },
  getMoneyStatistics() {
    Controllers.moneyStatistics().then(res => {
      const list = this.data.tab
      for (let i = 0; i < list.length; i++) {
        list[i].value = parseFloat(res.data[list[i].key]).toFixed(2)
      }
      this.setData({
        tab: list
      })
    });
  },
  changeTab(e) {
    var index = e.currentTarget.dataset.index;
    var type = this.data.tab[index].type;
    this.setData({
      currentIndex: index,
      type
    })
    this.moneyStatisticsPage();
  },
  changeChilds(e) {
    var index = e.currentTarget.dataset.index;
    var type = this.data.tab[this.data.currentIndex].childs[index].type;
    this.setData({
      childsIndex: index,
      type
    })
    this.moneyStatisticsPage();
  },

  startTimeChange(e) {
    this.setData({
      startTime: e.detail.value
    })
  },

  endTimeChange(e) {
    this.setData({
      endTime: e.detail.value
    })
  },

  moneyStatisticsPage(isFirst = true) {
    if (this.data.isLoading) {
      return;
    }
    this.setData({
      isLoading: true
    })
    wx.showLoading();


    if (isFirst) {
      this.setData({
        list: [],
        pageNumber: 1,
      })
    }
    let pageNumber = this.data.pageNumber;
    const endTime = this.data.endTime || '';
    const startTime = this.data.startTime || '';
    const types = this.data.type
    var value = {
      types,
      endTime,
      startTime,
      pageNumber,
      commission: 1
    };
    Controllers.moneyStatisticsPage(value).then(res => {
      console.log(res);
      let {
        list,
        lastPage
      } = res.data.page;
      
      list = this.data.list.concat(list);
      pageNumber++
      this.setData({
        list,
        lastPage,
        pageNumber,
        isLoading: false
      });
      wx.hideLoading();
    })
  },


  openPage(e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      app.openPage({
        url
      });
    } else {
      wx.showToast({
        title: '未获取页面路径',
        icon: 'none'
      });
    }
  },


  onReachBottom: function () {
    var that = this;
    if (that.data.isLoading || that.data.lastPage) {
      return;
    }

    that.moneyStatisticsPage(false);
  },

})